﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace GameVer1
{
    public class MyUnit
    {
        private int _X;
        public int X
        {
            get { return _X; }
            set { _X = value; }
        }
        private int _Y;
        public int Y
        {
            get { return _Y; }
            set { _Y = value; }
        }
        private Texture2D[] _UnitTexture;
        public Texture2D[] UnitTexture
        {
            get { return _UnitTexture; }
            set { _UnitTexture = value; }
        }
        private Vector2 _direction;
        public Vector2 Direction
        {
            get { return _direction; }
            set { _direction = value; }
        }
        private int _timeSinceLastFrame;
        public int TimeSinceLastFrame
        {
            get { return _timeSinceLastFrame; }
            set { _timeSinceLastFrame = value; }
        }
        private int _timePerFrame;
        public int TimePerFrame
        {
            get { return _timePerFrame; }
            set { _timePerFrame = value; }
        }
        private int _currentFrame;
        public int CurrentFrame
        {
            get { return _currentFrame; }
            set { _currentFrame = value; }
        }
        private int _numberFrame;
        public int NumberFrame
        {
            get { return _numberFrame; }
            set { _numberFrame = value; }
        }
        private Point _SizeFrame;
        public Point SizeFrame
        {
            get { return _SizeFrame; }
            set { _SizeFrame = value; }
        }
        private Vector2 _speed;
        public Vector2 Speed
        {
            get { return _speed; }
            set { _speed = value; }
        }
        private Color _filterColor;
        public Color FilterColor
        {
            get { return _filterColor; }
            set { _filterColor = value; }
        }
        private Rectangle _coverRect;
        public Rectangle CoverRect
        {
            get { return _coverRect; }
            set { _coverRect = value; }
        }
        private int _StopX;
        public int StopX
        {
            get { return _StopX; }
            set { _StopX = value; }
        }
        private float scale;
        public float Scale
        {
            get { return scale; }
            set { scale = value; }
        }
        public bool UpdateEnable;
        public SpriteEffects spriteEffect;
        public MyUnit()
        {
            this.CurrentFrame = 0;
            this.TimePerFrame = 40;
            this.TimeSinceLastFrame = 0;
            this.Direction = new Vector2(0,0);
            this.Speed = new Vector2(3.5f, 2.0f);
        }
        public MyUnit(Texture2D[] texture, int timesincrelastframe, int timeprtframe, Vector2 position,Vector2 speed,float fscale)
        {
            X = 0;
            Y = 0;
            UpdateEnable = true;
            spriteEffect = SpriteEffects.None;
            this.Scale = fscale;
            this.CurrentFrame = 0;
            this.TimePerFrame = 40;
            this.TimeSinceLastFrame = 0;
            this.UnitTexture = texture;
            this.Direction = position;
            this.SizeFrame = new Point(UnitTexture[0].Width, UnitTexture[0].Height);
            this.Speed = speed;
            this.FilterColor = Color.White;
        }
        public void Update(GameTime gameTime)
        {
            if (UpdateEnable == true)
            {
                TimeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
                if (TimeSinceLastFrame > TimePerFrame)
                {
                    TimeSinceLastFrame = TimeSinceLastFrame - TimePerFrame;

                }
                CurrentFrame += 1;
                //UpdateCoverRect();
            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            CurrentFrame = CurrentFrame%UnitTexture.Length;
            spriteBatch.Draw(UnitTexture[CurrentFrame],Direction,
                    new Rectangle(0, 0, UnitTexture[CurrentFrame].Width, UnitTexture[CurrentFrame].Height), Color.White, 0, Vector2.Zero, Scale, SpriteEffects.None, 0);
        }

    }
}
